<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/15
 * Time: 17:40
 * 后台用户管理
 */
class UserController extends AdminController{
    /**
     * 管理员列表
     */
    public function actionList(){
        $p = $this->get('p', 1);
        $group_id = $this->get('group_id',0);
        $username = $this->get('nickname');
		
        $criteria = new CDbCriteria();
        if ($group_id) 
        {
            $criteria->addColumnCondition(array('group_id'=> $group_id));
        }
        $criteria->addCondition('`status`=1');
        if ($username) {
            $criteria->addSearchCondition('username', $username);
        }
        $total = ManageUser::model()->count($criteria);
        $criteria->limit = $this->limit;
        $criteria->offset = ($p -1) * $this->limit;
        $criteria->order = 'create_time desc';
        $data = ManageUser::model()->findAll($criteria);

        //用户管理的项目
        if ($data) {
            $userproject = new ManageUserProject();
            $project = new Project();
            foreach ($data as $key => $val) {
                $project_ids = $userproject->getByUser($val['user_id']);

                if ($project_ids) {
                    if (-1 == $project_ids) {
                        $data[$key]['projects'] = '所有项目';
                    } else {
                        //根据项目id的到项目信息
                        $criteria = new CDbCriteria();
                        $criteria->addInCondition('project_id', $project_ids);
                        $project_list = $project->findAll($criteria);

                        if ($project_list) {
                            $plist = array();
                            foreach ($project_list as $k => $v) {
                                $plist[] = $v['project_name'];
                            }
                            $data[$key]['projects'] = join('，', $plist);
                        }
                    }
                }          
            }
        }
        $page = new Page($total, $this->limit);
        $allgroup = ManageGroup::model()->getAll();
        $this->smarty->assign('allgroup',$allgroup);
        $this->smarty->assign('page', $page->style1());
        $this->smarty->assign('nickname', $username);
        $this->smarty->assign('list', $data);
        $this->smarty->assign('group_id', $group_id);
        $this->smarty->display('user/list.tpl');
    }
    /**
     * 添加后台用户
     */
    public function actionAdd(){
        $group = new ManageGroup();
        $allgroup = $group->findAll('status=1');
        $this->smarty->assign('allgroup', $allgroup);
        $this->smarty->assign('op_name','添加管理员');
        $this->smarty->display('user/add.tpl');
    }
    /**
     * 添加
     */
    public function actionDoadd(){
        $user = new ManageUser();
        $username = $this->post('username');
        $user->username = $username;
        $user->truename = $this->post('truename');
        $user->email = $this->post('email');
        $user->phone = $this->post('phone');
        $user->group_id = $this->post('group_id');
        $project_id = $this->post('project_id');
        $user->pwd = md5($this->post('pwd'));
        $user->create_time = time();
        $pwd1 = md5($this->post('pwd1'));
        if (!empty($user->pwd) && $pwd1!=$user->pwd) {
            $this->jump('两次密码不一致');
        }
        //判断是否存在此用户
        if (ManageUser::model()->find("username='{$username}'")) {
            $this->jump('用户名已存在，请重新填写');
        }
        $res = $user->save();
        if ($res) {
            //拥有所有项目权限
            if ($project_id == -1) {
                $userproject = new ManageUserProject();
                $userproject->user_id = $user->user_id;
                $userproject->project_id = -1;
                $userproject->save();
            }
            $this->jump('保存成功', '/webadmin/user/list');
        } else {
            $this->jump('保存失败');
        }
    }
    /**
     * 编辑
     */
    public function actionEdit(){
        $user_id = (int)$this->get('user_id');
        $user = new ManageUser();
        $info = $user->findByPk($user_id)->attributes;
        $group = new ManageGroup();
        $allgroup = $group->findAll('status=1');
        //查找用户是否所有权限用户
        $userproject = new ManageUserProject();
        if ($userproject->count("user_id={$user_id} and project_id=-1")){
            $this->smarty->assign('project_id', -2);
        }    
        $this->smarty->assign('allgroup', $allgroup);
        $this->smarty->assign('info', $info);
        $this->smarty->assign('op_name','编辑管理员');
        $this->smarty->display('user/add.tpl');
    }
    /**
     * 修改
     */
    public function actionDoedit(){
        $data['username'] = $this->post('username');
        $data['truename'] = $this->post('truename');
        $data['email'] = $this->post('email');
        $data['phone'] = $this->post('phone');
        $data['group_id'] = (int)$this->post('group_id');
        $data['update_time'] = time();
        $user_id = (int)$this->post('user_id');
        $project_id = $this->post('project_id');
        
        $data['pwd'] = $this->post('pwd');
        $pwd1 = $this->post('pwd1');
        if (!empty($data['pwd']) && $pwd1!=$data['pwd']) {
            $this->jump('两次密码不一致');
        }
        //判断是否存在此用户
        if (ManageUser::model()->find("username='{$username}'")) {
            $this->jump('用户名已存在，请重新填写');
        }
        $user = new ManageUser();
        $info = $user->findByPk($user_id)->attributes;
        //如果修改密码了
        if (md5($data['pwd']) != $info['pwd']) {
            $data['pwd'] = md5($data['pwd']);
        } else {
            unset($data['pwd']);
        }
        $res = $user->updateByPk($user_id,$data);
        if ($res) {
            ManageUserProject::model()->deleteAll('user_id='.$user_id.' and project_id=-1');
            if ($project_id == -1) {
                $userproject = new ManageUserProject();
                $userproject->user_id = $user_id;
                $userproject->project_id = -1;
                $userproject->save();
            }
            $this->jump('修改成功', '/webadmin/user/list');
        } else {
            $this->jump('修改失败');
        }
    }

    /**
     * 修改当前用户个人信息
     */
    public function actionChange(){
        $user = new ManageUser();
        $info = $user->findById($this->user_id);
        $this->smarty->assign('info', $info);
        $this->smarty->display('user/change.tpl');
    }
    /**
     * 修改当前登录用户信息
     */
    public function actionDoChange(){
        $data['truename'] = $this->post('truename');
        $data['email'] = $this->post('email');
        $pwd = $this->post('pwd');
        if ($pwd) {
            $data['pwd'] = md5($pwd);
        }
        $user = new ManageUser();
        $res = $user->updateByPk($this->user_id,$data);
        if ($res) {
            $this->jump('修改成功', '/webadmin/user/change');
        } else {
            $this->jump('修改失败');
        }
    }
    /**
     * 删除用户
     */
    public function actionDel() {
            $user_id = $this->get('user_id');
            $data['status'] = 0;
            $res = ManageUser::model()->updateByPk($user_id, $data);
            if ($res) {
                    $this->_ajax(array('status' => 1,'note' => '删除成功'));
            } else {
                    $this->_ajax(array('status' => 0,'note' => '删除失败'));
            }
    }
}